package com.xncoding.freemarker.utile;

import org.springframework.util.StringUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串转为驼峰格式 构建工具类
 * @author LYY
 * @date 2022/07/06
 */

public class RegexUtils {

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    public static void main(String[] args) {

        System.out.println(lowerLineToHump("NAME_AND_addREsS"));
        System.out.println(capitalizeTheFirstLetter("nAMe"));
        System.out.println(humpToLowerLine("nameAndAddress"));
        System.out.println(humpToLowerLine("LoginQrcGen",true));
        System.out.println(humpToLowerLine("loginQrcGen",true));

    }

    /**
     * @Description: 下划线格式 -> 驼峰  大小写均可
     * <p>
     * NAME_AND_ADDRESS -> nameAndAddress
     * @Author: Yiang37
     * @Date: 2020/11/03 15:26:02
     * @Version: 1.0
     * @method: lowerLineAndUppercaseToHump()
     * @param: [Big_]
     * @return: java.lang.String
     */
    public static String lowerLineToHump(String lowerLineAndUppercaseStr) {
        //拆分成数组
        String[] eachStr = lowerLineAndUppercaseStr.split("_");
        StringBuilder resStr = new StringBuilder();
        String firstStr = "";
        String tempStr = "";
        for (int i = 0; i < eachStr.length; i++) {
            //第一个数组全部小写
            if (i == 0) {
                firstStr = eachStr[0].toLowerCase();
                resStr.append(firstStr);
            } else {
                //以后的数组首字母大写
                tempStr = capitalizeTheFirstLetter(eachStr[i]);
                resStr.append(tempStr);
            }
        }

        return resStr.toString();
    }

    /**
     * @Description: 任意字符串 -> 首字母大写
     * NAME -> Name
     * name -> Name
     * NaMe -> Name
     * @Author: Yiang37
     * @Date: 2020/11/03 16:50:16
     * @Version: 1.0
     * @method: capitalizeTheFirstLetter()
     * @param: [str]
     * @return: java.lang.String
     */
    public static String capitalizeTheFirstLetter(String str) {
        char firstChar = str.toUpperCase().charAt(0);
        String nextStr = str.toLowerCase().substring(1);
        return firstChar + nextStr;
    }

    /**
     * @Description: 驼峰 -> 下划线格式 默认小写,存在第二个形参且为true时大写.
     * @Author: Yiang37
     * @Date: 2020/11/03 17:10:25
     * @Version: 1.0
     * @method: humpToLowerLine()
     * @param: [humpStr, UppercaseZeroAndLowercaseOne]
     * @return: java.lang.String
     */
    public static String humpToLowerLine(String humpStr, boolean ... defaultUppercaseAndTrueLowercase) {
        if(isContainChinese(humpStr)){
            return "";
        }

        Matcher matcher = humpPattern.matcher(humpStr);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);

        //如果第二个形参为true 转为大写
        if (defaultUppercaseAndTrueLowercase.length>=1 && defaultUppercaseAndTrueLowercase[0]){
            String str = sb.toString().toUpperCase();
            if(str.startsWith("_")){
                return str.substring(1,str.length());
            }else {
                return str;
            }

        }
        return sb.toString();
    }
    /**
     * 字符串是否包含中文
     *
     * @param str 待校验字符串
     * @return true 包含中文字符  false 不包含中文字符
     * @throws EmptyException
     */
    public static boolean isContainChinese(String str) {

        if (StringUtils.isEmpty(str)) {
            return true;
        }
        Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\！|\\，|\\。|\\（|\\）|\\《|\\》|\\“|\\”|\\？|\\：|\\；|\\【|\\】]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }
    //首字母大写
    public static String captureName(String name) {
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
        return  name;
    }
    //首字母大写
    public static String uncaptureName(String name) {
        name = name.substring(0, 1).toLowerCase() + name.substring(1);
        return  name;
    }

}
